<?php

class AdsAction extends CommonAction{
	public function index(){
		$this->display();
	}

	/***************index1 (广告分类)******************/
	public function index1(){

		$condition['type'] = 'homeads';

		$temp =M('ad')->where($condition)->order('id ASC')->select();

		$list = cate_tree_list($temp,0);

		$this->assign("list",$list);

		$this->display();
	}


	public function add1(){

		if(isset($_POST['submit'])){
				
			$data['type'] = 'homeads';
				
			$data['content'] = $this->upfile();
			
			$data['remark'] = $_POST['remark'];
			
			$data['url'] = $_POST['url'];
			
		    $data['width'] = $_POST['width'];
			
			$data['height'] = $_POST['height'];
				
			$data['cityid'] = 0;
			$data['endtime'] = strtotime($_POST['endtime']);
				
			if(M('ad')->add($data)){

				$this->success("添加成功！",U('index1'));

			}else{

				$this->error("添加失败！",U('index1'));

			}
				
		}else{
			$this->display();

		}
	}


	public function edit1(){

		$id = empty($_GET['id'])?exit():$_GET['id'];

		$list = M('ad')->where("id=".$id)->find();

		if(isset($_POST['submit'])){
				
			$data['type'] = $list['type'];
			
			$data['content'] = $_POST['content'];
				
			$data['width'] = $_POST['width'];
			
			$data['height'] = $_POST['height'];

			$data['remark'] = $_POST['remark'];
			
			$data['url'] = $_POST['url'];
            $data['endtime'] = strtotime($_POST['endtime']);
				
			M('ad')->where("id=".$id)->save($data);

			$this->success("编辑成功！",U('index1'));

		}else{

			$this->li =M('ad')->where("id=".$id)->find();

			$this->display();
		}
			
	}


	public function del1(){
			        $qstr = $_GET['qstr'];
			        dump($qstr);
  		  	$data=M('ad');  		  
			if(isset($qstr)){
			   $arr=explode(",",$qstr);

			   foreach($arr as $val){

			     $data->where("id=$val")->delete();
			       
			   }
			   $this->success("删除成功！",U('index1'));			

			}else{
		

		$id = empty($_GET['id'])?exit():$_GET['id'];

		if(isset($id)){

			M('ad')->where("id=".$id)->delete();

			$this->success("删除成功！",U('index1'));

		}else{

			$this->error("删除失败！",U('index1'));

		}
			}
		
	}

	/***************index2 (城市广告分类)******************/

	public function index2(){
		$condition['type'] = 'cityads';

		$temp =M('ad')->where($condition)->order('id ASC')->select();

		foreach($temp as $v){
				if(empty($v['cityid'])){
                    $v['cityname'] = "全部城市";
                }else{
                    $v['cityname'] = M('city')->where('id='.$v['cityid'])->getField('city');
                }

				
			$list[]=$v;
		}

		$this->assign("list",$list);

		$this->display();
	}


	public function edit2(){

		$id = empty($_GET['id'])?exit():$_GET['id'];

		if(isset($_POST['submit'])){


			if(empty($_FILES['photo']['name'])){
                $data['content'] = $_POST['content'];
            }else{
                $data['content'] = $this->upfile();
            }

				
			$data['remark'] = $_POST['remark'];
				
			$data['cityid'] = $_POST['city'];

            $data['endtime'] = strtotime($_POST['endtime']);
			$data['url'] = $_POST['url'];
			


			M('ad')->where("id=".$id)->save($data);

			$this->success("编辑成功！",U('index2'));

		}else{

			$this->info =M('ad')->where("id=".$id)->find();
			$province = M('City')->where("status=0 AND province !='' ")->select();
			$this->assign("province",$province);

			$this->display();
		}
			
	}


	public function add2(){

		if(isset($_POST['submit'])){


			$data['type'] = 'cityads';
			$data['content'] = $this->upfile();
			$data['remark'] = $_POST['remark'];
			$data['cityid'] = $_POST['city'];
			$data['url'] = $_POST['url'];
            $data['endtime'] = strtotime($_POST['endtime']);
			if(M('ad')->add($data)){
				$this->success("添加成功！",U('index2'));
			}else{
				$this->error("添加失败！",U('index2'));
			}
		}else{

			$province = M('City')->where("status=0 AND province !='' ")->select();

			$this->assign("province",$province);
			$this->display();
		}
	}


	/*得到城市*/
	function getcity(){
			
		$id = $_GET['id'];
			
		$list = M('city')->where("cid=".$id)->select();
			
		echo(json_encode($list));
			
	}



	public function del2(){
		$qstr = $_GET['qstr'];
  		  	$data=M('ad');  		  
			if(isset($qstr)){
			   $arr=explode(",",$qstr);

			   foreach($arr as $val){

			     $data->where("id=$val")->delete();
			       
			   }
			   $this->success("删除成功！",U('index2'));			

			}else{
		
		
		$id = empty($_GET['id'])?exit():$_GET['id'];

		if(isset($id)){
			M('ad')->where("id=".$id)->delete();
			$this->success("删除成功！",U('index2'));
		}else{
			$this->error("删除失败！",U('index2'));
		}
			}
	}


    public function del3(){
        $qstr = $_GET['qstr'];
        $data=M('ad');
        if(isset($qstr)){
            $arr=explode(",",$qstr);

            foreach($arr as $val){

                $data->where("id=$val")->delete();

            }
            $this->success("删除成功！",U('index2'));

        }else{


            $id = empty($_GET['id'])?exit():$_GET['id'];

            if(isset($id)){
                M('ad')->where("id=".$id)->delete();
                $this->success("删除成功！",U('index2'));
            }else{
                $this->error("删除失败！",U('index2'));
            }
        }
    }
	/***************index3 (内容详情页面广告)******************/

	public function index3(){

		$condition['type'] = array('not in','cityads,homeads');

		$temp =M('ad')->where($condition)->order('id ASC')->select();

		foreach($temp as $v){

			$v['cityname'] = M('city')->where('id='.$v['cityid'])->getField('city');

			$list[]=$v;
		}
		$this->assign("list",$list);

		$this->display();

	}
    public function add3(){

        if(isset($_POST['submit'])){


            $data['type'] = '详情页顶部';
            $data['content'] = $this->upfile();
            $data['remark'] = $_POST['remark'];

            $data['url'] = $_POST['url'];
            $data['endtime'] = strtotime($_POST['endtime']);

            if(M('Ad')->add($data)){
                $this->success("添加成功！",U('index3'));
            }else{
                $this->error("添加失败！",U('index3'));
            }
        }else{


            $this->display();
        }
    }
	public function edit3(){
		$id = empty($_GET['id'])?exit():$_GET['id'];
		$list = M('ad')->where("id=".$id)->find();
		if(isset($_POST['submit'])){
			$data['type'] = $list['type'];

            if(empty($_FILES['photo']['name'])){
                $data['content'] = $_POST['content'];
            }else{
                $data['content'] = $this->upfile();
            }

			$data['remark']=$_POST['remark'];
			$data['cityid']= $list['cityid'];
			$data['width'] = $_POST['width'];			
			$data['url'] = $_POST['url'];
            $data['endtime'] = strtotime($_POST['endtime']);
			$data['height'] = $_POST['height'];

			M('ad')->where("id=".$id)->save($data);

			$this->success("编辑成功！",U('index3'));

		}else{

			$this->li =M('ad')->where("id=".$id)->find();

			$this->display();
		}
			
	}

    function logo(){
    	$id = empty($_GET['id'])?exit():$_GET['id'];
      	 if(isset($_POST['tijiao'])){
	     	 $data['content']=$this->upfile();
          if( M('Ad')->where("id=".$id)->setField('content', $data['content'])){
      	      $this->success("更新配置成功");
		   }else{
		    $this->error("更新配置错误");
		   }
		   
	  		}
		}

}
?>